package com.wuxiaofan.vo;

import lombok.Data;
import org.apache.commons.lang3.StringUtils;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

@Data
public class ReturnResult implements Serializable {
    private String code;
    private String msg;
    private String error_code;
    private Object result;
    private String cause;
    private String token;

    public ReturnResult(){}

    public ReturnResult(String code, String msg, String error_code, Object result, String cause, String token){
        this.code = code;
        this.msg = msg;
        this.error_code = error_code;
        this.result = result;
        this.cause = cause;
        this.token = token ;
    }

    public static ReturnResult success(String msg, Object result,String token) {
        return new ReturnResult("200", msg, (String)null, result, (String)null, token);
    }
    public static ReturnResult success(String msg, Object result) {
        return new ReturnResult("200", msg, (String)null, result, (String)null, (String)null);
    }
    public static ReturnResult success(String msg) {
        return new ReturnResult("200", msg, (String)null, null, (String)null, (String)null);
    }
    public static ReturnResult success() {
        return new ReturnResult("200", "成功", (String)null, null, (String)null, (String)null);
    }

    public static ReturnResult failure() {
        return failure("失败", (String)null, null);
    }
    public static ReturnResult failure(String msg) {
        return failure(msg, (String)null, null);
    }
    public static ReturnResult failure(String msg, Object result) {
        return failure(msg, (String)null, result);
    }


    public static ReturnResult failure(String msg, String error_code, Object result) {
        return new ReturnResult("400", msg, error_code, result, (String)null, (String)null);
    }

    public boolean isSucceed() {
        return this.getCode() != null && "200".equals(this.getCode());
    }

    public Map<String, Object> render() {
        Map<String, Object> map = new HashMap();
        if (!StringUtils.isBlank(this.code)) {
            map.put("code", this.code);
        }

        if (!StringUtils.isBlank(this.msg)) {
            map.put("msg", this.msg);
        }

        if (!StringUtils.isBlank(this.error_code)) {
            map.put("error_code", this.error_code);
        }

        if (this.result != null) {
            map.put("result", this.result);
        }

        if (!StringUtils.isBlank(this.cause)) {
            map.put("cause", this.cause);
        }

        if (!StringUtils.isBlank(this.token)) {
            map.put("token", this.token);
        }

        return map;
    }

}